import { ipcRenderer, contextBridge } from 'electron'

// --------- Expose some API to the Renderer process ---------
contextBridge.exposeInMainWorld('ipcRenderer', {
  on(...args: Parameters<typeof ipcRenderer.on>) {
    const [channel, listener] = args
    return ipcRenderer.on(channel, (event, ...args) => listener(event, ...args))
  },
  off(...args: Parameters<typeof ipcRenderer.off>) {
    const [channel, ...omit] = args
    return ipcRenderer.off(channel, ...omit)
  },
  send(...args: Parameters<typeof ipcRenderer.send>) {
    const [channel, ...omit] = args
    return ipcRenderer.send(channel, ...omit)
  },
  invoke(...args: Parameters<typeof ipcRenderer.invoke>) {
    const [channel, ...omit] = args
    return ipcRenderer.invoke(channel, ...omit)
  },

  // You can expose other APTs you need here.
  // 设置本地存储
  onSetLocalStore: (key: any, value: any) => {
    ipcRenderer.invoke('onSetLocalStore', key, value);
  },
  // 监听窗口变化
  winTitleOp: (action: string, data: any ) => {
    ipcRenderer.invoke('winTitleOp', action, data);
  },
  // 登录注册切换
  onLoginOrRegister: (isLogin: boolean) => {
    ipcRenderer.invoke('onLoginOrRegister', isLogin);
  },
  // 登录成功
  onLoginSuccess: (config: any) => {
    ipcRenderer.invoke('onLoginSuccess', config);
  },

  // 重新登录
  onReLogin: () => {
    console.log("重新登录");
    ipcRenderer.invoke('onReLogin')
  },

  addToContact: (uid: number) => {
    console.log('uid===>', uid);
    ipcRenderer.invoke('onAddToContact', uid);
    console.log('uid===>', uid);
  }
})
